var eventCreate = {
    saveInfo: function(url, targetUrl) {
        if (accountType == "2") {
            $("input[name='is_allow']").change(function() {
                if ($("#event-limit").is(":checked")) {
                    var n = $(".upper-limit").val();
                    var fee = n * 15;
                    $("#tip").text('请确保账户有' + fee + '吼币，活动费用以实际参会人数为准，剩余金额将在次日退回到账户');
                    $(".upper-limit").removeAttr("disabled");
                } else {
                    $(".upper-limit").prop("disabled", "disabled");
                    $("#tip").text('');
                }
            });
            $(".upper-limit").keyup(function() {
                var n = $(".upper-limit").val();
                if (n < webinar_top) {
                    $("#tip").text('您当前不能减少参会人数');
                } else {
                    var fee = (n - webinar_top) * 15;
                    $("#tip").text('请确保账户有' + fee + '吼币，活动费用以实际参会人数为准，剩余金额将在次日退回到账户');
                }
            });
            $(".upper-limit").keydown(function(e) {
                vhallApp.isNumber(e);
            });
        }

        $(".input-sec").keydown(function(e) {
            vhallApp.isNumber(e);
        });
        $('input[name="buffer"]').change(function() {
            if ($(".event-delay").is(':checked')) {
                //var default_val = $('#delay-tips').attr('defalut'); 
                $('#delay-tips').text('');
                //$('.input-sec').removeAttr('disabled');
            } else {
                //$('.input-sec').attr('disabled','disabled');
                $('#delay-tips').text('该模式对发起方与观看方网络要求较高，请谨慎选择');
            }
        });
        var eventTitle = $("#event-title");
        var eventIntroduce = $("#event-introduce");
        var eventType = $("#event-type");

        eventType.change(function() {
            if ($(this).val() != "0" && $(this).hasClass("warning")) {
                $(this).removeClass("warning");
            }
        });
        $("#event-title,#event-introduce").each(function() {
            $(this).blur(function() {
                var target = $(this).parents(".form-group").find(".comment");
                if (!$(this).hasClass("warning") && target.hasClass("warning")) {
                    target.removeClass("warning");
                }
            });
        });
        $(".savebtn").click(function() {
            var completed = true;
            $("#event-title, #event-introduce").each(function() {
                var val = $(this).val();
                if (!vhallApp.waringNull($(this), $(this).attr("placeholder")) || (val == $(this).attr("placeholder"))) {
                    completed = false;
                    return false;
                }
            });
            if (completed) {
                if (strlen($.trim(eventTitle.val())) > 60) {
                    eventTitle.addClass("warning");
                    tmpText = eventTitle.val();
                    eventTitle.parents(".form-group").find(".comment").addClass("warning");
                    completed = false;
                    return false;
                }
            }
            if (completed) {
                if ($.trim(eventIntroduce.val()).length > 1000) {
                    eventIntroduce.addClass("warning");
                    tmpText = eventIntroduce.val();
                    eventIntroduce.parents(".form-group").find(".comment").addClass("warning");
                    completed = false;
                    return false;
                }
            }
            if (completed) {
                if (eventType.val() == "0") {
                    eventType.addClass("warning");
                    completed = false;
                    return false;
                }
            }
            if (completed) {
                $("[name='start_date'], [name='start_time']").each(function() {
                    var val = $(this).val();
                    if (!vhallApp.waringNull($(this)) || (val == $(this).attr("placeholder"))) {
                        completed = false;
                        return false;
                    }
                });
            }
            if (completed && (accountType == "2")) {
                if ($("#event-limit").is(":checked")) {
                    var targets = $(".upper-limit");
                    var upperLimit = targets.val();
                    if (!(/^\d{2,}$/).test(upperLimit) || parseInt(upperLimit) < 21) {
                        tmpText = upperLimit;
                        targets.addClass("warning").val("");
                        completed = false;
                        return false;
                    }
                }
            }
            /*if(completed){
                if($("#event-delay").is(":checked")) {
                    var targets=$(".input-sec");
                    var upperLimit=targets.val();
                    if(parseInt(upperLimit) > 120 || parseInt(upperLimit) < 1 || !(/^\d{1,}$/).test(upperLimit)){
                        tmpText=upperLimit;
                        targets.addClass("warning").val(""); 
                        completed=false;
                        $('#delay-warning').removeClass('hide');
                        return false;                           
                    }
                     $('#delay-warning').addClass('hide');
                }
            }*/
            if (completed) {
                var eventDatas = {
                    'subject': eventTitle.val(),
                    'introduction': eventIntroduce.val(),
                    'category': eventType.val(),
                    'img_url': $("#img_url").val(),
                    'start_date': $("[name='start_date']").val(),
                    'start_time': $("[name='start_time']").val(),
                    'buffer': parseInt($("input[name='buffer']").val())
                };
                $("input[type='radio'],input[type='checkbox']").each(function() {
                    if ($(this).is(":checked")) {
                        eventDatas[$(this).attr("name")] = $(this).val();
                    }
                    if ($(this).is("#event-private")) {
                        if ($(this).is(":checked")) {
                            eventDatas.is_open = 0;
                        } else {
                            eventDatas.is_open = 1;
                        }
                    }
                    if ($(this).is("#event-limit")) {
                        eventDatas.top = $(".upper-limit").val();
                    }
                });
                //console.log(eventDatas);return;
                $.ajax({
                    url: url,
                    type: 'post',
                    data: eventDatas,
                    success: function(res) {
                        var status = res.code;
                        switch (status) {
                            case "200":
                                //vhallApp.showMsg("保存成功！")
                                if (typeof res.data != "undefined") {
                                    window.location.href = targetUrl + res.data;
                                } else {
                                    window.location.href = targetUrl;
                                }
                                break;
                            case "1001":
                                var n = $(".upper-limit").val();
                                $("#tip").text('您的余额不足以发起' + n + '人的活动，请先充值');
                                break;
                            case "1002":
                                $("#tip").text('您当前不能减少参会人数');
                                break;
                            default:
                                break;
                        }
                    }
                });
            }
        });
    }
};

function strlen(str){
    var len = 0;
    for (var i=0; i<str.length; i++) { 
     var c = str.charCodeAt(i); 
    //单字节加1 
     if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) { 
       len++; 
     } 
     else { 
      len+=2; 
     } 
    } 
    return len;
}
